package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.TbAddress;
import com.dongdongshop.pojo.TbSeckillGoods;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.result.Result;
import com.dongdongshop.result.ResultEnum;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.service.SeckillService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("seckill")
public class SeckillController {

    private static final Logger logger = LoggerFactory.getLogger(SeckillController.class);

    @Reference(timeout = 10000,retries = 0)
    private SeckillService seckillService;

    @Reference
    private AddressService addressService;

    @RequestMapping("toSeckillShow")
    public String toSeckillShow(Model model){
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (null != tbUser){
            model.addAttribute("tbUser",tbUser.getUsername());
        }
        List<TbSeckillGoods> tbSeckillGoodsList = seckillService.getSeckillGoods();
        model.addAttribute("seckillGoods",tbSeckillGoodsList);
        return "seckill-index";
    }

    @RequestMapping("togetSecGoodById/{id}")
    public String togetSecGoodById(@PathVariable("id") Long id,Model model){
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (null != tbUser){
            model.addAttribute("tbUser",tbUser.getUsername());
        }
        TbSeckillGoods tbSeckillGoods = seckillService.getSeckillGoodById(id);
        model.addAttribute("seckill",tbSeckillGoods);
        return "seckill-item";
    }

    @RequestMapping("createSeckillOrder")
    @ResponseBody
    public Result createSeckillOrder(Long id){
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        try {
            seckillService.createSeckillOrder(id,tbUser.getUsername());
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(ResultEnum.GOODS_SALE_OVER);
        }
    }

    @RequestMapping("getOrderId")
    @ResponseBody
    public Result getOrderId(){
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        Long orderId = seckillService.getOrderId(tbUser.getUsername());
        if (null == orderId){
            return Result.jsonResponse(ResultEnum.NOT_FOUND);
        }

        return Result.jsonResponse(ResultEnum.OK).setData(orderId.toString());
    }

    @RequestMapping("insertSecOrderAddress")
    @ResponseBody
    public Result insertSecOrderAddress(Long addressId,Long id){
        try {
            TbAddress addressById = addressService.getAddressById(addressId);

            int i = seckillService.insertSecOrderAddress(addressById,id);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

}
